﻿<script type="text/javascript">
    $(function () {
        getButton("business");
    });

    function ui_business_init_list(toolbar) {
        $("#ui_business_dg").datagrid({       //初始化datagrid
            url: "ashx/bg_business.ashx?action=search",
            striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
            idField: 'id',
            sortName: 'Sort',
            sortOrder: 'asc',
            pageList: [20, 40, 60, 80, 100],
            frozenColumns: [[
                { field: 'deadName', title: '死者姓名', sortable: true, width: 100 },
                { field: 'relativeName', title: '亲属姓名', sortable: true, width: 100 }
            ]],
            columns: [[
                        { field: 'tel', title: '联系电话', sortable: true, width: 80 },
                       { field: 'businessType', title: '业务类别', sortable: true, width: 80 },
                       { field: 'estTime', title: '预约时间', sortable: true, width: 150 },
                       { field: 'estLocation', title: '预约地点', sortable: true, width: 100 },
                       {
                           field: 'content', title: '业务内容', sortable: true, width: 250,
                           formatter: function (value, row, index) {
                               return value.length > 20 ? '<span title="' + value + '">' + value + '</span>' : value;
                           }
                       },
                       { field: 'actTime', title: '实际时间', sortable: true, width: 150 },

                       {
                           field: 'result', title: '结果', sortable: true, width: 250,
                           formatter: function (value, row, index) {
                               if (!value) return "";
                               return value.length > 20 ? '<span title="' + value + '">' + value + '</span>' : value;
                           }
                       }

            ]],
            toolbar: toolbar.length == 0 ? null : toolbar,   //请求回来的当前用户可以操作的按钮
            onDblClickRow: function (rowIndex, rowData) {     //双击行弹框编辑
                ui_business_edit();
            }
        });

    }

    function ui_business_add() {
        $("<div/>").dialog({
            id: "ui_business_add_dialog",
            href: "html/ui_business_add.html",
            iconCls: "icon-add",
            title: "预约登记",
            height:330,
            width: 600,
            modal: true,
            buttons: [{
                id: "ui_business_add_btn",
                text: '添加',
                iconCls: "icon-add",
                handler: function () {
                    $("#ui_business_addform").form("submit", {
                        url: "ashx/bg_business.ashx",
                        onSubmit: function (param) {
                            
                            $('#ui_business_add_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'add';
                            if ($(this).form('validate')) {                                
                                return true;
                            }
                            else {
                                $('#ui_business_add_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_business_add_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_business_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_business_add_btn').linkbutton('enable');  //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_business_name_add").focus();
            },
            onClose: function () {
                $("#ui_business_add_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_business_edit() {
        var row = $("#ui_business_dg").datagrid("getChecked");
        if (row.length < 1) {
            $.show_warning("提示", "请先勾选要修改的行或者双击某行");
            return;
        }
        if (row.length > 1) {
            $.show_warning("提示", "不支持批量修改");
            $("#ui_business_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        $("<div/>").dialog({
            id: "ui_business_edit_dialog",
            href: "html/ui_business_edit.html",
            iconCls: "icon-edit",
            title: "修改预约登记信息",
            height: 330,
            width: 600,
            modal: true,
            buttons: [{
                id: "ui_business_edit_btn",
                text: '修 改',
                iconCls: "icon-edit",
                handler: function () {
                    $("#ui_business_editform").form("submit", {
                        url: "ashx/bg_business.ashx",
                        onSubmit: function (param) {
                            $('#ui_business_edit_btn').linkbutton('disable');   //点击就禁用按钮，防止连击
                            param.action = 'edit';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_business_edit_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_business_edit_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_business_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_business_edit_btn').linkbutton('enable');    //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {                
                $("#ui_business_id").val(row[0].id);
                $("#ui_business_dead_name").textbox('setValue', row[0].deadName);
                $("#ui_business_dead_cardno").textbox('setValue', row[0].deadCardNo);
                $('#ui_business_relativeId').val( row[0].relativeId);
                $('#ui_business_deadId').val(row[0].deadId);
                $('#ui_business_relative_name').textbox('setValue', row[0].relativeName);
                $("#ui_business_relative_tel").textbox('setValue', row[0].tel);
                $("#ui_business_businessType").combobox('setValue', row[0].businessType);
                $("#ui_business_estLocation").combotree('setValue', row[0].estLoaction);
                $("#ui_business_estTime").datetimebox('setValue', row[0].estTime);
                $("#ui_business_content").textbox('setValue', row[0].content);                
            },
            onClose: function () {
                $("#ui_business_edit_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_business_delete() {
        var rows = $("#ui_business_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的行");
            return;
        }
        $.messager.confirm('提示', '确定删除勾选的' + rows.length + '行？', function (r) {
            if (r) {
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();
                para.id = "";
                var ids = [];
                $.each(rows, function (i, row) {
                    ids.push(row.id);
                });
                para.id = ids.join(",");
                $.ajax({
                    url: "ashx/bg_business.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", data.msg);
                            $("#ui_business_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }
</script>
<div id="ui_business_layout" class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false">
        <table id="ui_business_dg" data-options="fit:true,border:false">
        </table>
    </div>
</div>
